//
//  BLHotelHomeViewController.m
//  blanket
//
//  Created by Mac on 2018/11/14.
//  Copyright © 2018 stoneobs@icloud.com. All rights reserved.
//

#import "BLHotelHomeViewController.h"
#import "BLHotelHomeTableViewCell.h"
#import "BLHotelDetailViewController.h"
#import "BLHotleOrderViewController.h"
@interface BLHotelHomeViewController ()
@property(nonatomic, strong) NSMutableArray                     *dataSouce;/**< <##> */
@end

@implementation BLHotelHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"铺盖酒店";
    __weak typeof(self) weakSelf =  self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf sendHotleListRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    STButton * orderButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 80, 80)
                                                     title:@"我的订单"
                                                titleColor:UIColor.whiteColor
                                                 titleFont:15
                                              cornerRadius:40
                                           backgroundColor:BL_BlueBackGroundColor
                                           backgroundImage:nil
                                                     image:nil];
    orderButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.view addSubview:orderButton];
    [orderButton setClicAction:^(UIButton *sender) {
        [weakSelf.navigationController pushViewController:BLHotleOrderViewController.new animated:YES];
    }];
    orderButton.bottom = UIScreenHeight - 150;
    orderButton.right = UIScreenWidth - 40;
    // Do any additional setup after loading the view.
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return BLHotelHomeTableViewCell.cellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    BLHotelHomeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[BLHotelHomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    BLHotelListModel * model = self.dataSouce[indexPath.row];
    cell.model = model;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     BLHotelListModel * model = self.dataSouce[indexPath.row];
    BLHotelDetailViewController * vc = BLHotelDetailViewController.new;
    vc.hid = model.h_id;
    vc.money = model.price;
    [self.navigationController pushViewController:vc animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --NetWork Method
- (void)sendHotleListRequest{
//    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"/api/hotel/hotelList/p/1"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 self.dataSouce = NSMutableArray.new;
                                                 NSArray * array = responseObject[@"data"];
                                                 for (NSDictionary * modelDic in array) {
                                                     BLHotelListModel * model = [BLHotelListModel mj_objectWithKeyValues:modelDic];
                                                     [self.dataSouce addObject:model];
                                                 }
                                                 [self.tableView reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
//                                                 [SVProgressHUD dismiss];
//                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}


@end
